package ca.ubc.cpsc210.waldo.translink;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import ca.ubc.cpsc210.waldo.exceptions.IllegalBusStopException;
import ca.ubc.cpsc210.waldo.model.Bus;
import ca.ubc.cpsc210.waldo.model.BusRoute;
import ca.ubc.cpsc210.waldo.model.BusStop;
import ca.ubc.cpsc210.waldo.util.LatLon;

public class TranslinkServiceFromJSONFile extends TranslinkService {
	
	@Override
	public void getBusStopsAround(LatLon location, int radius, Reader input) {
		
		int stopno;
		String name;
		LatLon latlon;
		List<String> loroute;
		Set<BusRoute> routes;
		
		int distance;
		
		TranslinkService service = new TranslinkServiceFromJSONFile();
		
			try {
				
//				FileReader input1 = new FileReader("./TestFiles/Student/UBCBusStopsJSON");
//				JSONArray jArr = (JSONArray) new JSONTokener(input1).nextValue();	
				
				JSONArray jArr = (JSONArray) new JSONTokener(input).nextValue();
				
				JSONObject jObj;
				
				for (int i = 0; i < jArr.length() ; ++i) {
					
					
					jObj = jArr.getJSONObject(i);
					stopno = jObj.getInt("StopNo");
					name = String.valueOf(jObj.get("Name")).trim();
					latlon = new LatLon(jObj.getDouble("Latitude"), jObj.getDouble("Longitude"));
					loroute = Arrays.asList((String.valueOf(jObj.get("Routes")).split("\\s*,\\s*")));
					routes = service.translateRoutes(loroute);
//					distance = jObj.getInt("Distance");
					
//					if (distance <= radius) {
						super.addBusStop(new BusStop(stopno, name, latlon, routes));
//						System.out.println("Added:" + stopno + ":" + name + ":" + latlon.getLatitude() + ":" + latlon.getLongitude() + ":" + routes.size());

//					}
					
				}

			} catch (JSONException e) {
				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
			}
		
		
	}

	
	
	
	
	
	@Override
	public void getBusEstimtesForStop(BusStop stop, Reader input) {
		
		String direction;		//Bus
		int minutesToDeparture;
		
		BusRoute route;
//		BusStop stop2;
		
		boolean cancelled;
		
//		Bus bus;
		
//		String number;			// BusRoute
		
		List<BusRoute> routes;
		
//		TranslinkService service = new TranslinkServiceFromJSONFile();
		
			try {
				
//				FileReader input1 = new FileReader("./TestFiles/Student/BusEstimatesFor61598-JSON");
//				JSONArray jArr = (JSONArray) new JSONTokener(input1).nextValue();
				
				JSONArray jArr = (JSONArray) new JSONTokener(input).nextValue();
				
				JSONObject jObj;
				
				routes = new ArrayList<BusRoute>();
				
				for (int i = 0; i < jArr.length() ; ++i) {
					
					jObj = jArr.getJSONObject(i);
					
					route = new BusRoute(jObj.getString("RouteNo").trim());
					direction = jObj.getString("Direction").trim();
					
					JSONArray schedule = jObj.getJSONArray("Schedules"); // array for schedules
					
					JSONObject jObj2; // deal with schedules
					for (int j = 0; j < schedule.length(); ++j) {
						jObj2 = schedule.getJSONObject(j);

						minutesToDeparture = (jObj2.getInt("ExpectedCountdown"));
						cancelled = jObj2.getBoolean("CancelledTrip");
//						System.out.println(cancelled);
						
						if (!cancelled) {
							route.addBus(new Bus(route, direction, stop, minutesToDeparture));
							System.out.println(route.toString() + " " + direction);
						}
					}
					routes.add(route);
//					System.out.println(route.getRouteNumber() + "no");
				}

				super.addToRoutes(routes);
				
				
			} catch (JSONException e) {
				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
			}
		
		
	}
	
	
	
	
	
	
	// ----------------EXAMPLE
	public static void main(String[] args) {
		
		TranslinkServiceFromJSONFile service = new TranslinkServiceFromJSONFile();
		
//		service.getBusStopsAround(new LatLon(49.26636, -123.24158), 500, null);
		service.getBusEstimtesForStop(new BusStop(61598), null);
		
//		int num = 0;
//		
//		System.out.println(num);
//
//		String str1 = "hello";
//
//			num = Integer.parseInt(str1);
//		
//		System.out.println(num);
		
		
//		String json = "{"
//		         + "  \"query\": \"Pizza\", "
//		         + "  \"locations\": [ 94043, 90210 ] "
//		         + "}";
//
//		 JSONObject object;
//		try {
//			object = (JSONObject) new JSONTokener(json).nextValue();
//			 String query = object.getString("query");
//			 JSONArray locations = object.getJSONArray("locations");
//			 
//			 System.out.println(query + " " + locations);
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
	}

}
